﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using exsportance.Silverlight.Common.Controls;

namespace exsportance.Silverlight.Common.Helper
{
    /// <summary>
    /// A content switcher
    /// </summary>
    public class ContentSwitcher
    {
        #region members
        private Panel _root;
        #endregion

        #region events
        public delegate void PageChangedDelegate(UserControl page);
        public event PageChangedDelegate PageChanged;
        #endregion

        #region constructor
        /// <summary>
        /// Static constructor
        /// </summary>
        public ContentSwitcher(Panel rootControl)
        {
            _root = rootControl;
        }
        #endregion

        #region public methods

        /// <summary>
        /// Switch to new page
        /// </summary>
        /// <param name="transition">Animation between switching</param>
        /// <param name="newPage"></param>
        public void Navigate(TransitionBase transition, UserControl newPage)
        {
            UserControl oldPage = getOldPage();

            if (oldPage == null)
                Navigate(newPage);
            else
            {
                _root.Children.Insert(0, newPage);

                transition.TransitionCompleted += transition_TransitionCompleted;
                transition.PerformTranstition(newPage, oldPage);
            }
        }

        /// <summary>
        /// Switch to new page without animation
        /// </summary>
        /// <param name="newPage"></param>
        public void Navigate(UserControl newPage)
        {
            UserControl oldPage = getOldPage();

            _root.Children.Add(newPage);
            if (oldPage != null)
            {
                _root.Children.Remove(oldPage);
                if (oldPage is IDisposable)
                    (oldPage as IDisposable).Dispose();
                oldPage = null;
            }

            if (PageChanged != null)
                PageChanged(newPage);
        }

        /// <summary>
        /// Reload the current page
        /// </summary>
        /// <param name="transition"></param>
        public void ReloadCurrentPage(TransitionBase transition)
        {
            UserControl oldPage = getOldPage();
            if (oldPage != null)
            {
                // create a new instance of current
                UserControl newPage = Activator.CreateInstance(oldPage.GetType()) as UserControl;

                Navigate(transition, newPage);

                if (oldPage is IDisposable)
                    (oldPage as IDisposable).Dispose();
                oldPage = null;
            }
        }

        /// <summary>
        /// Get the current visible page
        /// </summary>
        /// <returns></returns>
        public UserControl GetCurrentPage()
        {
            return getOldPage();
        }
        #endregion

        #region private methods
        private void transition_TransitionCompleted(object sender, TransitionCompletedEventArgs e)
        {
            if (e.OldPage != null)
                _root.Children.Remove(e.OldPage);

            if (PageChanged != null)
                PageChanged(e.NewPage);
        }

        private UserControl getOldPage()
        {
            UserControl result = null;

            if (_root.Children.Count == 2) // DebugWindow included
            {
                if (_root.Children[0] is DebugWindow)
                    result = _root.Children[1] as UserControl;
                else
                    result = _root.Children[0] as UserControl;
            }
            else
            {
                if (_root.Children.Count > 0)
                    result = _root.Children[0] as UserControl;
            }


            return result;
        }
        #endregion
    }
}
